<?php
/*
Plugin Name: 用户动态
Plugin URI: http://wpjam.net/item/wpjam-basic/
Description: 监测注册用户的行为。
Version: 1.0
*/
global $wpdb;
$wpdb->activities		= $wpdb->base_prefix . 'activities';
$wpdb->login_faileds	= $wpdb->base_prefix . 'login_faileds';

function wpjam_activity($args=array()){
	global $wpdb;

	$args = wp_parse_args($args, array(
		'blog_id'	=> get_current_blog_id(),	// 操作的博客
		'user_id'	=> get_current_user_id(),	// 用户
		'type'		=> '',						// 类型
		'hook'		=> current_filter(),		// 接口
		'note'		=> '', 						// 注释
		'object'	=> 0,						// 操作的对象，可以是 post_id, comment_id, user_id 等
		'time'		=> time(),
		'url'		=> wpjam_get_current_page_url(),
		'ip'		=> wpjam_get_ip(),
		'ua'		=> wpjam_get_ua(),
	) );

	$args['note'] = maybe_serialize($args['note']);

	if(!$args['type']) {
		return new WP_Error('empty_type','动态类型不能为空');
	}

	if(!$args['user_id'])  {
	 	return new WP_Error('empty_user','user_id不能为空');
	}

	if($wpdb->insert($wpdb->activities, $args)){
		return $wpdb->insert_id;
	}else{
		return new WP_Error('insert_failed', $wpdb->last_error);
	}	
}

// 登录失败
add_action('wp_login_failed','wpjam_activity_login_failed');
function wpjam_activity_login_failed($user_name){
	global $wpdb;
	$wpdb->insert($wpdb->login_faileds, array(
		'blog_id'	=> get_current_blog_id(),
		'user_name'	=> $user_name,	
		'time'		=> time(),
		'url'		=> wpjam_get_current_page_url(),
		'ip'		=> wpjam_get_ip(),
		'ua'		=> wpjam_get_ua(),
	));
}

//成功登录
add_action('wp_login', 'wpjam_activity_login', 10, 2);
function wpjam_activity_login($user_login, $user){
	$user_data	= (array)$user->data;
	wpjam_activity(	array(
		'type'		=>'login', 
		'user_id'	=>$user->ID,
		'note'		=>$user_data['user_nicename']
	) );
}

// 退出
// add_action('wp_logout', 'wpjam_activity_logout');
// function wpjam_activity_logout(){
// 	if(!empty($_COOKIE['wpjam_logged'])){
// 		setcookie( 'wpjam_logged', '', time() - YEAR_IN_SECONDS );
// 	}
// }



add_action('profile_update', 'wpjam_activity_profile_update', 10, 2);
function wpjam_activity_profile_update($user_id, $old_user_data){
	$user_data	= get_userdata( $user_id );
	$user_data	= (array)$user_data->data;
	$old_user_data	= (array)$old_user_data;

	if(array_diff_assoc($old_user_data, $user_data)){
		$note	= array(array_diff_assoc($old_user_data, $user_data), array_diff_assoc($user_data, $old_user_data));
	}else{
		$note	= '';
	}
	
	if(get_current_user_id() != $user_id){
		wpjam_activity( array(
			'type'		=>'edit_user', 
			'object'	=>$user_id,
			'note'		=>$note
		) );
	}else{
		wpjam_activity(array(
			'type'		=>'profile_update', 
			'object'	=>$user_id,
			'note'		=>$note
		) );
	}
}

add_action('user_register', 'wpjam_activity_user_register');
function wpjam_activity_user_register($user_id){
	if(get_current_user_id() != $user_id){
		wpjam_activity( array(
			'type'		=>'add_user', 
			'object'	=>$user_id
		) );
	}else{
		wpjam_activity(array(
			'type'		=>'user_register', 
			'object'	=>$user_id
		) );
	}
}

add_action( 'delete_user', 'wpjam_activity_delete_user' );
add_action( 'wpmu_delete_user', 'wpjam_activity_delete_user' );
function wpjam_activity_delete_user($user_id){
	wpjam_activity( array(
		'type'		=>'delete_user', 
		'object'	=>$user_id
	) );
}

add_action( 'add_user_to_blog', 'wpjam_activity_add_user_to_blog', 10, 3);
function wpjam_activity_add_user_to_blog($user_id, $role, $blog_id){
	wpjam_activity( array(
		'type'		=>'add_user_to_blog', 
		'object'	=>$user_id, 
		'blog_id'	=>$blog_id, 
		'note'		=>get_role($role)->name 
	) );
}

add_action( 'remove_user_from_blog', 'wpjam_activity_remove_user_from_blog',10,2);
function wpjam_activity_remove_user_from_blog($user_id, $blog_id){
	wpjam_activity(array(
		'type'		=>'remove_user_from_blog', 
		'object'	=>$user_id, 
		'blog_id'	=>$blog_id 
	) );
}

// /*日志动态*/
// add_action( 'post_updated', 'wpjam_activity_post_updated', 10, 3 );
// function wpjam_activity_post_updated($post_ID, $post_after, $post_before){
// 	if($post_after->post_status == $post_before->post_status){
// 		wpjam_activity(array('type'=>'edit_post', 'object'=>$post_ID, 'note'=>$post_before->post_status ) );
// 	}else{
// 		if($post_after->post_status == 'publish'){
// 			wpjam_credit(array('type'=>'publish_post', 'object'=>$post_ID, 'credit_user'=>$post_after->post_author, 'note'=> $post_before->post_status) );
// 		}else{
// 			wpjam_activity(array('type'=>$post_after->post_status.'_post', 'object'=>$post_ID, 'note'=> $post_before->post_status) );
// 		}
// 	}
// }

// add_action( 'save_post', 'wpjam_activity_save_post', 10, 3 );
// function wpjam_activity_save_post($post_ID, $post, $update){
// 	if($update) return;
// 	if($post->post_status == 'publish'){
// 		wpjam_credit(array('type'=>'publish_post', 'object'=>$post_ID, 'credit_user'=>$post->post_author ) );
// 	}else{
// 		wpjam_activity(array('type'=>'add_post', 'object'=>$post_ID, 'note'=> $post->post_status ) );
// 	}
// }

// add_action( 'delete_post', 'wpjam_activity_delete_post');
// function wpjam_activity_delete_post($post_ID){
// 	$delete_post = get_post($post_ID);
// 	wpjam_activity(array('type'=>'delete_post', 'object'=>$post_ID, 'note'=> $delete_post->post_title) );
// }

// add_action('comment_post', 'wpjam_activity_comment_post', 10, 2);
// function wpjam_activity_comment_post($comment_ID, $comment_approved){
// 	if(get_current_user_id()){
// 		if($comment_approved){
// 			wpjam_credit(array('type'=>'post_comment', 'object'=>$comment_ID ) );
// 		}else{
// 			wpjam_activity(array('type'=>'post_comment', 'object'=>$comment_ID, 'note'=> $comment_approved ) );
// 		}
// 	}
// }

// add_action('transition_comment_status', 'wpjam_activity_transition_comment_status', 10, 3);
// function wpjam_activity_transition_comment_status($new_status, $old_status, $comment){
// 	$type = ($new_status == 'approved' && $old_status != 'unapproved')? 'un'.$old_status.'_comment' : $new_status.'_comment';
// 	$note = ($new_status == 'delete')?$comment->comment_content : $old_status;
// 	wpjam_activity(array('type'=>$type, 'object'=>$comment->comment_ID, 'note'=> $note ) );
// }

// add_action('edit_comment', 'wpjam_activity_edit_comment');
// function wpjam_activity_edit_comment($comment_ID){
// 	wpjam_activity(array('type'=>'edit_comment', 'object'=>$comment_ID ) );
// }

// add_action('switch_theme', 'wpjam_activity_switch_theme', 10, 2);
// function wpjam_activity_switch_theme($new_name, $new_theme ){
// 	wpjam_activity(array('type'=>'switch_theme', 'object'=>$new_name, 'note'=>get_option('theme_switched') ) );
// }

// add_action('activate_plugin', 'wpjam_activity_activate_plugin', 10, 2);
// add_action('deactivate_plugin', 'wpjam_activity_activate_plugin', 10, 2);
// function wpjam_activity_activate_plugin($plugin, $network_wide){
// 	if(strpos($plugin, 'wpjam-basic.php')) return;
// 	wpjam_activity(array('type'=>current_filter(), 'object'=>$plugin, 'note'=>$network_wide ) );
// }

// add_action('updated_option', 'wpjam_activity_updated_option', 10, 3);
// function wpjam_activity_updated_option($option, $old_value, $value){
// 	if( in_array( $option, array( 'current_theme','theme_switched', 'template', 'stylesheet', 'active_plugins', 'recently_activated' ) ) ) return;
// 	wpjam_activity(array('type'=>'updated_option', 'object'=>$option, 'note'=>array('value'=>$value,'old_value'=>$old_value) ) );
// }

// add_action('added_option', 'wpjam_activity_added_option', 10, 2);
// function wpjam_activity_added_option($option, $value){
// 	wpjam_activity(array('type'=>'added_option', 'object'=>$option, 'note'=>$value ) );
// }




// function wpjam_credit($args=array()){

// 	global $wpdb;

// 	$args = wp_parse_args($args, array(
// 		'credit_user'	=> get_current_user_id(),		
// 		'credit_change'	=> 0, 		// 改动的积分
// 		'exp_change'	=> false, 	// 改动的经验值
// 		'multiple'		=> 1,		// 删除的倍数
// 		'object'		=> 0,		// 加分的对象，可以是 post_id, comment_id, user_id
// 		'type'			=> '',		// 类型
// 	) );

// 	extract($args);

// 	if(!$type) {
// 		return new WP_Error('unknown-type','未知动态类型');
// 	}

// 	$credit_limit = 100;
// 	$credit_changes = array(
// 		'login'			=> '10',
// 		'publish_post'	=> '10',
// 		'post_comment'	=> '5'
// 	);

// 	$credit_change = ($credit_change)?$credit_change:(isset($credit_changes[$type])?$credit_changes[$type]:0);
// 	$credit_change = (int)$credit_change * (int)$multiple;
	
// 	$limit = 0;

// 	if($credit_change > 0 && $type != 'manual' ){ // 积分兑换，扣分，和管理员加分就不检测
// 		$current_time	= current_time('timestamp');
		
// 		$today_credit_sum =  (int)$wpdb->get_var($wpdb->prepare("SELECT SUM(credit_change) FROM {$wpdb->activities} WHERE credit_user=%d AND credit_change > 0 AND type != 'manual' AND ( ( YEAR( time ) = %d AND MONTH( time ) = %d AND DAYOFMONTH( time ) = %d ) );",$credit_user, date('Y',$current_time), date('m',$current_time), date('d',$current_time)));

// 		if($today_credit_sum >= $credit_limit){ // 超过积分上限
// 			$credit_change = 0;
// 			$limit = 1;
// 		}elseif($wpdb->query($wpdb->prepare("SELECT id FROM {$wpdb->activities} WHERE credit_user=%d AND type=%s AND object=%d", $credit_user, $type, $object))){ // 同一操作只加一次分
// 			$credit_change = 0;
// 			$limit = 2;
// 		}
// 	}

// 	if($exp_change === false){ // 传递进来 0 就不加
// 		$exp_change = $credit_change;
// 	}

// 	unset($args['multiple']);

// 	$old_credit	= (int)get_user_meta( $credit_user, 'credit', 'true' );
// 	$old_exp	= (int)get_user_meta( $credit_user, 'exp', 'true' );

// 	$credit = $old_credit + $credit_change;
// 	$exp 	= $old_exp + $exp_change;

// 	$args['credit_change']	= $credit_change;
// 	$args['credit']			= $credit;
// 	$args['exp_change']		= $exp_change;
// 	$args['exp']			= $exp;
// 	$args['limit']			= $limit;

// 	$result = wpjam_activity($args);

// 	if(is_wp_error($result)){
// 		return $result;
// 	}else{
// 		update_user_meta( $credit_user, 'credit', $credit );
// 		update_user_meta( $credit_user, 'exp', $exp );
// 		return $credit_change;
// 	}
// }

